<?php
namespace app\index\controller\user;

use app\common\model\admin\AccountModel;
use app\common\model\paths\ClassTaskModel;
use app\common\model\paths\TaskChapterModel;
use app\common\model\paths\TaskModel;
use app\common\model\paths\UserClassModel;
use app\index\controller\BaseIndex;
use think\admin\model\SystemUser;

/**
 * 我的课程
 * @package app\index\controller\user
 * @author wangqinghui
 * @DateTime 2024-09-12 11:22:14
 */
class Course extends BaseIndex
{

    // 强制登录
    protected $need_login = true;

    /**
     * 我学习的课程列表
     * @author wangqinghui
     * @DateTime 2024-09-12 11:25:33
     * @return void
     */
    public function index()
    {
        $this->title = lang('我的实践课程');
        $this->fetch('index', ['page_size' => 16]);
    }

    /**
     * 获取课程列表
     * @author wangqinghui
     * @DateTime 2024-09-13 18:41:13
     * @return void
     */
    public function list() {
        // 搜索内容
        $key = trim($this->request->post('key', ''));
        // 分页
        $page = (int)$this->request->post('page', 1);
        $limit = (int)$this->request->post('limit', 16);

        // 获取我的班级
        $map = [
            ['index_user_id', '=', $this->userInfo['id']],
            ['status', '=', UserClassModel::STATUS_AGREE]// 申请通过
        ];
        $classList = UserClassModel::mk()->field('index_user_id,class_id,platform_id,user_id')->where($map)->select()->toArray();
        $classData = array_column($classList, 'class_id');
        $courseList = [];
        $total = 0;
        if(!empty($classData)){
            // 获取班级下的课程
            $map = [
                ['class_id', 'in', $classData],
            ];
            $taskList = ClassTaskModel::mk()->where($map)->select()->toArray();
            $taskIds = array_column($taskList, 'task_id');
            if(!empty($taskIds)){
                // 获取课程信息
                $map = [
                    ['id', 'in', $taskIds],
                    ['status', '=', 1],
                ];
                if(!empty($key)){
                    $map[] = ['title', 'like', "%{$key}%"];
                }
                $total = TaskModel::mk()->where($map)->count();
                $courseList = TaskModel::mk()->where($map)->page($page,$limit)->order('id desc')->select()->toArray();
                if(!empty($courseList)){
                    // 获取章节总数
                    $ids = array_column($courseList, 'id');
                    $map = [
                        ['task_id', 'in', $ids],
                        ['status', '=', 1],// 启用
                        ['pid', '=', 0]
                    ];
                    $chapter_total_list = TaskChapterModel::mk()->field('task_id, count(id) as num')->where($map)->group('task_id')->select()->toArray();
                    // 获取课程作者信息
                    $author_ids = array_column($courseList, 'user_id');
                    $author_data = [];
                    if(!empty($author_ids)){
                        $map = [
                            ['id',  'in', $author_ids],
                            ['status', '=', 1],
                            ['usertype', '=', 'teacher']
                        ];
                        $field = 'id,nickname,headimg';
                        $author_data = SystemUser::mk()->where($map)->column($field, 'id');
                    }

                    // 获取课程所属平台
                    $platform_ids = array_column($courseList, 'account_id');
                    $platform_data = [];
                    if(!empty($platform_ids)){
                        $map = [
                            ['id',  'in', $platform_ids],
                            ['status', '=', 1],
                            ['module',  'find in set', AccountModel::MODULE_SCHOOLS]
                        ];
                        $field = 'id,title';
                        $platform_data = AccountModel::mk()->where($map)->column($field, 'id');
                    }
                    $chapter_total = array_column($chapter_total_list, 'num', 'task_id');
                    foreach ($courseList as $key => $item){
                        $courseList[$key]['chapter_total'] = $chapter_total[$item['id']] ?? 0;
                        $courseList[$key]['auther'] = !empty($author_data[$item['user_id']]['nickname']) ? $author_data[$item['user_id']]['nickname'] : '';
                        $courseList[$key]['platform'] = !empty($platform_data[$item['account_id']]['title']) ? $platform_data[$item['account_id']]['title'] : '';
                    }
                }
            }
        }
        $this->success('获取成功', ['total' => $total, 'list' => $courseList]);
    }
}